This is an introduction to Spatial Analysis in R. I’ve developed this code based on some common questions from friends and colleagues or ones that I’ve asked myself. There is a lot here to help you get started, but there is also a lot more to learn!
The focus here will be on raster analysis, rather than vector (shapefiles, polygons, polylines, points, etc.).
I’ve drafted this informal introductory session in the form of answering a scientific question….
Where are optimal sites for Sea Monkey aquaculture off the west coast?
We will answer this question by taking into consideration the following spatial data:
1. Sea Surface Temperature 2. Net Primary Productivity 3. Marine Protected Areas
Key information for optimal growth:
Raster or gridded data are stored as a grid of value which are rendered on a map as pixels. Each pixel value represents an area on the Earth’s surface.
Some examples of raster data include oceanographic datasets such as Sea Surface Temperature, land use maps and digital elevation maps.
Raster data can come in many different formats. In this tutorial, we will use the geotiff format which has the extension .tif. A .tif file stores metadata or attributes about the file as embedded tif tags. These tags can include the following raster metadata:
CRS)extent)NoDataValue)resolution of the dataInformation in this section is borrowed from NEON’s Intro to Raster Data in R tutorial, another great resource
There are a lot of spatial packages for R, we will touch on some of them here but not all of them. Here is brief overview, taken from this site:
raster package is also able to read NetCDF files and I prefer to use Raster whenever possible.Load all libraries:
library(raster) #Main raster library with nearly all functions used in this analysis
library(rgdal) #Spatial library - most functions used from rgdal are for vectors (shapefiles)
library(rasterVis) #Useful for raster visualizations
library(maps) #Has a database of maps. I use this to add a map to my raster to visualize land boundaries
library(rgeos) #Need this library for topology operations on geometries
library(dplyr) #NOT spatial - this is a data wrangling library
library(RColorBrewer) #Also not spatial - used to set the spectral color scheme
For raster data visualization I prefer the spectral color scheme rather than the base graphics package. I’m also setting the plotting margins much smaller so that the plots will show up larger in the viewing pane.
# rainbow color scheme
cols = rev(colorRampPalette(brewer.pal(11, 'Spectral'))(255))
#setting margins for plotting
par(mar=c(2,2,1,1))
My first step in a spatial analysis is prepping the data, which includes the following:
Read in a shapefile of the US West Coast and northern Baja peninsula by using readOGR from the rgdal package.
# dsn is the path name & layer is the name of the file. NOTE: you do not need to add an extension to the layer name
#named cc for california current
cc = readOGR(dsn='data',layer='ca_current')
## OGR data source with driver: ESRI Shapefile
## Source: "data", layer: "ca_current"
## with 2 features
## It has 7 fields
Plotting a shapefile is just as easy as:
plot(cc)
And to add land to the map, do the following (from the maps package)
plot(cc)
#add a landmap to your shapefile
map('world',fill=T,add=T,col='gray')
The information in the summary of the shapefile is important if you need to understand what projection your SpatialPolygonsDataFrame is in, along with the extent and number of features. In this case there are just two features, the EEZs of the US West Coast and northern Baja Mexico. You can get this information by just typing in the name ‘cc’
cc
## class : SpatialPolygonsDataFrame
## features : 2
## extent : -129.1635, -109.9721, 21.6185, 49.08721 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## variables : 7
## names : EEZ, Country, ID, Sovereign, Remarks, Sov_ID, Last_Chang
## min values : Mexican Exclusive Economic Zone, Mexico, 135, Mexico, NA, 135, 30/03/2006
## max values : United States Exclusive Economic Zone, United States, 163, United States, NA, 163, 30/03/2006
You can look at the data held within a shapefile by calling cc@data. The dataframe associated with the shapefile can be treated just as any other dataframe. Columns can be added, names can be changed, tables can be joined.
cc@data
## EEZ Country ID Sovereign
## 0 United States Exclusive Economic Zone United States 163 United States
## 1 Mexican Exclusive Economic Zone Mexico 135 Mexico
## Remarks Sov_ID Last_Chang
## 0 <NA> 163 30/03/2006
## 1 <NA> 135 30/03/2006
#Add another column:
cc@data$short = c('USA','MEX')
cc@data
## EEZ Country ID Sovereign
## 0 United States Exclusive Economic Zone United States 163 United States
## 1 Mexican Exclusive Economic Zone Mexico 135 Mexico
## Remarks Sov_ID Last_Chang short
## 0 <NA> 163 30/03/2006 USA
## 1 <NA> 135 30/03/2006 MEX
Now that we have our boundary area defined by the shapefile, we can start prepping the raster data.
In the Data folder, there are 5 .tif files with the naming pattern average_annual_sst_[year].tif, which are 5 annual average sea surface temperatures for our region (2008-2012). We want just one raster file of the average SST over that time period.
To create a single average Sea Surface Temperature layer, do the following:
sst_files = list.files('data',pattern='average_',full.names = T) #We need full file paths
sst_files
## [1] "data/average_annual_sst_2008.tif" "data/average_annual_sst_2009.tif"
## [3] "data/average_annual_sst_2010.tif" "data/average_annual_sst_2011.tif"
## [5] "data/average_annual_sst_2012.tif"
Create a raster of the first file by calling raster() and then plot() to visualize.
#This function reads in raster files. Think of it as similar to read.csv()
r = raster(sst_files[1])
#remember cols was defined at the beginning to plot values in the spectral color scheme
plot(r,col=cols)
Notice the data values are in Kelvin - we will change this to celsius later.
You can plot rasters or shapefiles on top of each other
plot(r,col=cols)
plot(cc,add=T)
I also like to look at the distribution of data. Using the histogram() function from rasterVis is my preference over hist() from the base package purely because of the visual output.
histogram(r)
To get a single layer of average SST in degrees Celsius we need to first stack all layers.
#stack is a function from the raster package that puts all RasterLayers into a RasterStack
sstStack = stack(sst_files)
plot(sstStack,col=cols)
You can perform operations on a RasterStack by using the calc() function from the raster package. calc() lets you define a function to apply across all layers in the stack.
Calculate the mean value per cell and then convert to Celsius by subtracting 273.15.
# By adding 'filename=' R will directly save the raster into the defined file rather than memory
sstAvg = calc(sstStack,fun=function(x){mean(x,na.rm=T)-273.15})#,filename='data/sstAvg.tif', overwrite=T)
plot(sstAvg,col=cols, main = 'Mean Sea Surface Temperature (Celsius)');plot(cc,add=T)
A more compact way of doing multiple raster analysis is by using pipes…you can run stack() and calc() in one call!
sstAvg = stack(sst_files)%>%
calc(.,fun=function(x){mean(x,na.rm=T)-273.15})
plot(sstAvg,col=cols, main = 'Mean Sea Surface Temperature (Celsius)');plot(cc,add=T)
Read in this data the same way as the SST data, using raster(). This data is the net primary production (mgC/m2/day).
npp = raster('data/annual_npp.tif');npp
## class : RasterLayer
## dimensions : 145, 114, 16530 (nrow, ncol, ncell)
## resolution : 112000, 54700 (x, y)
## extent : -17813502, -5045502, 1526148, 9457648 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs
## data source : C:\Users\Afflerbach\Documents\github\spatial-analysis-R\data\annual_npp.tif
## names : annual_npp
## values : 1.898059, 3.783287 (min, max)
plot(npp,col=cols, main = 'Net Primary Production (mgC/m2/day)')
You’ll see that this is in a different projection, extent and cell size from the SST data. It is really obvious when you look at the plot, but the summary above also gives clues as to what projection/resolution/extent this data is in.
To do any sort of analysis using multiple rasters, they all need to be in the same extent, projection and cell resolution.
First look at the differences:
sstAvg
## class : RasterLayer
## dimensions : 720, 648, 466560 (nrow, ncol, ncell)
## resolution : 0.04166185, 0.04165702 (x, y)
## extent : -131.9848, -104.9879, 19.99537, 49.98842 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +no_defs
## data source : in memory
## names : layer
## values : -1.57, 38.24 (min, max)
npp
## class : RasterLayer
## dimensions : 145, 114, 16530 (nrow, ncol, ncell)
## resolution : 112000, 54700 (x, y)
## extent : -17813502, -5045502, 1526148, 9457648 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=moll +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs
## data source : C:\Users\Afflerbach\Documents\github\spatial-analysis-R\data\annual_npp.tif
## names : annual_npp
## values : 1.898059, 3.783287 (min, max)
To get the primary productivity data in the same format as the SST data, we need to
reprojectcropresampleUse projectRaster() from the raster package to reproject a RasterLayer from one projection to another. You will need to define what the new projection should be by setting a coordinate reference system.
Defining a coordinate reference system (crs) can be done in many ways. See Melanie’s great cheat sheet for more details about Coordinate Reference Systems.
Here, we want to project from Mollweide to longlat
nppProj = projectRaster(npp,crs = ('+proj=longlat'))
#You will see a warning about finite points. I'm not entirely sure what this warning means but I suspect it has to do with warping of some cells when reprojecting. I've googled it and it seems like this is a harmless warning.
plot(nppProj,col=cols)
Now that the layer is in the right projection, we need to crop it to our study area and make sure all raster layers have the same extent.
You can set the extent using extent(). You can also use another raster object to define the extent (in this case we will use sstAvg)
nppCrop = crop(nppProj,sstAvg) #crop nppProj to the extent of sstAvg
plot(nppCrop,col=cols);plot(cc,add=T)
Just by plotting both the SST and OA data, you can tell right away that these two datasets have different cell resolutions. The NPP data needs to be resampled to the same cell size as SST in order to do any sort of analysis on these two. Use the nearest neighbor method to avoid interpolation between points. We want to keep the data values the same as the original data, but at a higher resolution.
Here you can see the difference:
#include progress='text' in any raster function to see a progress bar as the function runs! This is one of my favorite R things!
npp_res = resample(nppCrop,sstAvg,method='ngb')#,progress='text')
npp_bil = resample(nppCrop,sstAvg,method='bilinear')#,progress='text')
par(mfrow=c(1,2))
plot(npp_res,col=cols);plot(npp_bil,col=cols)
dev.off() # This turns off the graphic device to stop plotting in 2x1 format
## null device
## 1
NOTE: Typically you’ll want to disaggregate cells to match data of a higher resolution. Otherwise, if we aggregate the cells from the SST data, we would lose data.
Again we can condense this script by using pipes!
npp = projectRaster(npp,crs=('+proj=longlat'))%>%
crop(.,sstAvg)%>%
resample(.,sstAvg,method='ngb')#,progress='text')
plot(npp,col=cols)
Check to see that we can use the SST and NPP data together now
stack(npp,sstAvg) #No error and the stack has two layers so it looks good!
## class : RasterStack
## dimensions : 720, 648, 466560, 2 (nrow, ncol, ncell, nlayers)
## resolution : 0.04166185, 0.04165702 (x, y)
## extent : -131.9848, -104.9879, 19.99537, 49.98842 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +ellps=WGS84 +no_defs
## names : annual_npp, layer
## min values : 2.302361, -1.570000
## max values : 3.487666, 38.240000
Now that our data is prepped and guaranteed to play nicely, we can move onto the fun stuff - analyzing the data. For this specific analysis, we need to use the SST and NPP data to find areas within the cc region that are suitable for growing seamonkeys. This requires removal of all cells from NPP and SST that are not within the ideal growth parameter range.
Remove all cells from the Sea Surface Temperature layer that fall out of the species temperature range
Seamonkeys grow best in waters that are between 12 and 18 degrees Celsius.
Remembering that sstAvg is our current SST layer, you can eliminate all cells with values outside of your range in a few different ways.
I prefer to do a simple subset using brackets, but first assigning a new variable so you don’t overwrite sstAvg
plot(sstAvg,col=cols);plot(cc,add=T)
# trying something like this gives you all of the cell values that fit your condition. Not exactly what we want
#sstPref=sstAvg[sstAvg>=12 & sstAvg<=18]
#I prefer the following
sstPref = sstAvg #rename variable so we maintain the original sstAvg.
sstPref[sstPref<12 | sstPref>18]<-NA #set all cells outside of our range to NA using brackets to subset
plot(sstPref,col=cols, main = 'Mean Sea Surface Temperature (Celsius)');plot(cc,add=T)
Remove all cells from the Ocean Acidification layer that fall out of the species preferred range
Our species also prefers water with a mean primary production of between 2.6 and 3 mgC/m2/day
plot(npp,col=cols)
nppPref <- npp
nppPref[nppPref>3|nppPref<2.6]<-NA
plot(nppPref,col=cols, main = 'Net Primary Production (mgC/m2/day)'); plot(cc,add=T)
Now that we have our two rasters with suitable parameters, there are a lot of different ways to select overlapping cells. I like to do this in a binary way all suitable cells set equal to 1, the rest NA or 0.
Set all viable cells in SST to equal 1
sstBin = sstPref #assigning new variable sstBin (binary) so that sstPref is not overwritten
sstBin[!is.na(sstBin)]<-1 #setting all cells that are not NA to 1
plot(sstBin, col='coral', main='Areas with suitable SST');plot(cc,add=T)
…and same for NPP
nppBin = nppPref
nppBin[!is.na(nppBin)]<-1
plot(nppBin,col='darkorchid2',main='Areas with Suitable NPP');plot(cc,add=T)
Now that we have these two binary layers, we can combine them using overlay() from the raster package and the resulting cells, equal to 1, are the cells that meet both SST and NPP requirements
cells = overlay(sstBin,nppBin,fun=function(x,y){x*y})
plot(cells,col='lightblue',main='Suitable Aquaculture Areas');plot(cc,add=T)
***
SIDE NOTE:
You can perform mathematical operations on single or multiple raster layers using base R functions. Both calc() and overlay() are useful when you have complex functions working on these layers. Here are some examples of how you can use raster layers with base R functions:
#sum
sum = sum(sstBin,nppBin,na.rm=T);plot(sum) #gives you cells equal to 0, 1 and 2.
#power
power = sstAvg^4;plot(power,col=cols)
#average
avg = mean(sstAvg,npp);plot(avg,col=cols)
***
This could be all you need - but I want to show some additional steps to highlight more functionality in R.
You can remove cells outside of your region by using the mask() function. Here, you only want to keep cells that are within the Mexican or US EEZs.
plot(cells, col='lightblue');plot(cc,add=T)
cellsEEZ = mask(cells,cc)
plot(cellsEEZ,col='lightblue', main = 'Suitable Aquaculture Areas');plot(cc,add=T)
You can then crop your raster to a smaller extent
cellsCrop = crop(cellsEEZ,extent(-125,-110,25,35)) #setting extent by eyeing it
plot(cellsCrop,col='lightblue',main = 'Suitable Aquaculture Areas');plot(cc,add=T)
Another nifty thing - if you don’t know the extent, you can draw it and R will tell you! You can then save this extent and crop to it
plot(cellsEEZ,col='lightblue');plot(cc,add=T)
ext <- drawExtent(show=TRUE,col='red')
#class : Extent
#xmin : -126.729
#xmax : -113.3571
#ymin : 27.48709
#ymax : 35.00825
ext=extent(-126.729,-113.3571,27.48709,35.00825) #I picked this one,
cropCells=crop(cellsEEZ,ext)
plot(cropCells,col='lightblue', main = 'Suitable Aquaculture Areas');plot(cc,add=T)
There are two shapefiles in the data folder. One is ‘MPA_State’ which are state MPAs, and one is ‘National Marine Sanctuaries’. To go along with the aquaculture analysis, lets say that cells within these regions must be excluded from the suitability analysis.
#marine protected areas
mpa = readOGR(dsn='data',layer='MPA_State',verbose=F);mpa
## class : SpatialPolygonsDataFrame
## features : 101
## extent : -373440.2, 259220.6, -590425.8, 259522.3 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
## variables : 7
## names : NAME, Type, Length_nmi, CCR, Area_nmi, Perimeter_, present
## min values : Año Nuevo, SMCA, 0.00, Section 632 (b)1, 0.01000000, 0.560000, 1
## max values : White Rock (Cambria), Special Closure, 15.36, Yet to be published, 24.79270395, 31.336239, 1
plot(mpa, main = 'Marine Protected Areas along the West Coast')
# try to crop to our extent
mpa_c = crop(mpa,ext);mpa_c
## NULL
Notice that when creaing mpa_c there is no error. But when you call it the output is NULL, indicating a NULL object. This is because the MPA shapefile is not in the same projection
Using spTransform() from the rgdal package, you can project a shapefile in a similar manner as raster.
mpa = spTransform(mpa,CRS("+proj=longlat")) #spTransform is part of rgdal package
#crop again
mpa_c = crop(mpa,ext);plot(mpa_c, main = "Marine Protected Areas");plot(cc,add=T)
The same can be done with the National Marine Sanctuary shapefile, using pipes!
#national marine sanctuaries
nms = readOGR(dsn='data',layer='National Marine Sanctuaries',verbose=F)%>%
spTransform(.,CRS("+proj=longlat"))%>%
crop(.,ext)
plot(nms, main = 'National Marine Sanctuaries');plot(cc,add=T)
To remove cells in the MPAs and NMS just use the mask() function but set inverse=T so that all cells outside of the polygons are kept and those inside are set to NA.
cellsAQ = mask(cropCells,mpa_c,inverse=T)%>%
mask(.,nms,inverse=T) #by setting inverse=T, all cells that do not overlap with the mask are kept and those overlapping the mask are set to NA
plot(cellsAQ,col='lightblue', main = 'Suitable Aquaculture Areas');plot(cc,add=T)
With any raster analysis, you likely aren’t just creating a pretty map. Here is an example of running zonal statistics on a raster.
We want to look at the total area (km2) in Mexico and California for seamonkey aquaculture.
First you want to turn a shapefile (california current) into a raster of the same projection/extent/resolution as your cells.
#let's take a look at what the CC dataframe looks like again
head(cc)
## EEZ Country ID Sovereign
## 0 United States Exclusive Economic Zone United States 163 United States
## 1 Mexican Exclusive Economic Zone Mexico 135 Mexico
## Remarks Sov_ID Last_Chang short
## 0 <NA> 163 30/03/2006 USA
## 1 <NA> 135 30/03/2006 MEX
ccZones = rasterize(cc,cellsAQ)#,progress='text')
ccZones
## class : RasterLayer
## dimensions : 180, 321, 57780 (nrow, ncol, ncell)
## resolution : 0.04166185, 0.04165702 (x, y)
## extent : -126.7354, -113.3619, 27.49363, 34.99189 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## data source : in memory
## names : layer
## values : 1, 2 (min, max)
## attributes :
## ID EEZ Country ID.2 Sovereign
## 1 United States Exclusive Economic Zone United States 163 United States
## 2 Mexican Exclusive Economic Zone Mexico 135 Mexico
## Remarks Sov_ID Last_Chang short
## <NA> 163 30/03/2006 USA
## <NA> 135 30/03/2006 MEX
plot(ccZones,col=cols, main='Shapefile rasterized into zones (US and Mexico)')
R automatically assigned 1 and 2 as the cell values. You can set your own values too based on a field or another defined vector of ids.
#using a field from the shapefile
cc_ras_ID = rasterize(cc,cellsAQ,field="ID");cc_ras_ID
## class : RasterLayer
## dimensions : 180, 321, 57780 (nrow, ncol, ncell)
## resolution : 0.04166185, 0.04165702 (x, y)
## extent : -126.7354, -113.3619, 27.49363, 34.99189 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
## data source : in memory
## names : layer
## values : 135, 163 (min, max)
plot(cc_ras_ID, col=cols)
To get the total viable area for aquaculture of seamonkeys in the California Current, run zonal() using ccZones. The zonal() function is given any sort of function you define including sum, mean, max, etc.
Since the current values of prefRas are all equal to 1, we can simply sum up the number of cells in each EEZ.
par(mfrow=c(1,2))
plot(cellsAQ,col='lightblue',main='Suitable Aquaculture Areas');plot(ccZones,col=cols,main='US and Mexico zones')
cellsSum = zonal(cellsAQ,ccZones,fun='sum') #total number of cells since they are all equal to 1
cellsSum
## zone sum
## [1,] 1 10663
## [2,] 2 4725
dev.off()
## null device
## 1
But that isn’t as useful as calculating the actual area in km2. Using the area() function from the raster package you can create a new raster with cell values equal to their area, and then run zonal().
cellsArea = area(cellsAQ,na.rm=T);plot(cellsArea,col=cols, main='Cell area (km2)') #gives are in km2
area = zonal(cellsArea,ccZones,fun='sum');area
## zone sum
## [1,] 1 190867.6
## [2,] 2 87307.5